<?php
require_once LIBS_DIR . '/DatePicker.php';
class Admin_LidePresenter extends BasePresenter
{	
	public function renderDefault()
	{
		
		$lide = new LideModel();
		$this->template->lide = $lide->findAll();
	}
	
	public function renderNovy()
	{
		$form = $this['lideForm'];
	}
	
	public function renderUpravit($id = 0)
	{
		$form = $this['lideForm'];
		if (!$form->isSubmitted()) {
			$lide = new LideModel();
			$row = $lide->findById($id)->fetch();
			$this->template->image = $row['fotka'];
			if (!$row) {
				throw new NBadRequestException('Záznam nebyl nalezen.');
			}
			$form->setDefaults($row);
		}
	}
	
	public function renderSmazat($id = 0)
	{
		$lide = new LideModel();
		$this->template->lide = $lide->findById($id)->fetch();
		if (!$this->template->lide) {
			throw new NBadRequestException('Člověk nebyl nalezen.');
		}
	}
	
	protected function createComponentSmazatForm()
	{
		$form = new NAppForm;
		
		$form->addSubmit('delete', 'Smazat')->getControlPrototype()->class('default');
		$form->addSubmit('cancel', 'Zpět');
		$form->onSubmit[] = callback($this, 'smazatFormSubmitted');
		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		return $form;
	}
	
	public function smazatFormSubmitted(NAppForm $form)
	{
		if ($form['delete']->isSubmittedBy()) {
			$lide = new LideModel();
			$id = $this->getParam('id');
			$image_name = $lide->findById($id)->fetch();
			@unlink('./images/fotka_uzivatele/'.$image_name['fotka']);
			$lide->delete($id);
			$this->flashMessage('Člověk byl odstraněn.');
		}

		$this->redirect('default');
	}
	
	protected function createComponentLideForm()
	{
		$form = new NAppForm;
		$form->addText('jmeno', 'Jméno:');
		$form->addText('prijmeni', 'Příjmení:');
		$form->addText('prezdivka', 'Přezdívka:');
		$form->addDatePicker('narozen', 'Datum narození:');
		$form->addText('vaha', 'Váha:');
		$form->addText('vyska', 'Výška:');
		$form->addText('telefon', 'Telefon:');
		$form->addText('email', 'E-mail:');
		$form->addTextArea('info', 'Info:');
		$role_items = array('admin'=>'Administrátor','editor'=>'Klubový editor','uživatel'=>'Uživatel');
		$form->addSelect('role', 'Role:', $role_items)->skipFirst('Zvolte roli');
		$form->addText('heslo', 'Heslo:');
		
		$form->addFile('fotka', 'Foto:');
	
		$form->addSubmit('save', 'Uložit')->getControlPrototype()->class('default');
		$form->addSubmit('cancel', 'Zpět')->getControlPrototype()->class('cancel')->setValidationScope(NULL);
		$form->onSubmit[] = callback($this, 'lideFormSubmitted');

		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		$this->template->form = $form;
		return $form;
	}
	public function lideFormSubmitted(NAppForm $form)
	{
		if ($form['save']->isSubmittedBy()) {
			$id = (int) $this->getParam('id');
		
			$image = $form['fotka']->value;
			$lide = new LideModel();
			if ($id > 0) {
				$nazev = $this->uploadImages($id, $image, 'update');
				if($nazev == ''){
					$image_name = $lide->findById($id)->fetch();
					$nazev = $image_name['fotka'];
				}
				
				$values = array(
					'jmeno'=>$form['jmeno']->value,
					'prijmeni'=>$form['prijmeni']->value,
					'prezdivka'=>$form['prezdivka']->value,
					'narozen'=>$form['narozen']->value,
					'vaha'=>$form['vaha']->value,
					'vyska'=>$form['vyska']->value,
					'telefon'=>$form['telefon']->value,
					'email'=>$form['email']->value,
					'info'=>$form['info']->value,
					//'heslo'=>md5($form['heslo']->value),
					'role'=>$form['role']->value,
					'fotka'=>$nazev
				);
				$lide->update($id, $values);
				
				$this->flashMessage('Člověk byl upraven.');
			} else {

				$values = array(
					'jmeno'=>$form['jmeno']->value,
					'prijmeni'=>$form['prijmeni']->value,
					'prezdivka'=>$form['prezdivka']->value,
					'narozen'=>$form['narozen']->value,
					'vaha'=>$form['vaha']->value,
					'vyska'=>$form['vyska']->value,
					'telefon'=>$form['telefon']->value,
					'email'=>$form['email']->value,
					'info'=>$form['info']->value,
					'heslo'=>md5($form['heslo']->value),
					'role'=>$form['role']->value,
					'fotka'=>'noimage.jpg'
				);
				
				$lide->insert($values);
				$id = dibi::getInsertId();
				$nazev = $this->uploadImages($id, $image);
				$lide->update($id, array('fotka'=>$nazev));
				$this->flashMessage('Člověk byl vytvořen.');
			}
		}

		$this->redirect('default');
	}
	public function uploadImages($id, $image, $operation = 'insert'){
		if($image->isOk()){
			$mimeType = $_FILES['fotka']['type'];
			switch($mimeType){
				case 'image/jpeg': $type = ".jpg";
				break;
				case 'image/gif': $type = ".gif";
				break;
				case 'image/png': $type = ".png";
				break;
				case 'image/tif': $type = ".tif";
				break;
				case 'image/tiff': $type = ".tiff";
				break;
				case 'image/bmp': $type = ".bmp";
				break;
			}

			$uploaddir = WWW_DIR . '/images/fotka_uzivatele/';
			$filesMain = scandir(WWW_DIR.'/images/fotka_uzivatele');
			$name = $id;    
			if($operation == 'update'){
				$lide = new LideModel();
				$image_name = $lide->findById($id)->fetch();
				@unlink('../images/fotka_uzivatele/'.$image_name['fotka']);
			}
			$uploadfile = $uploaddir . $name . $type;
			$image = NImage::fromFile($_FILES['fotka']['tmp_name']);
			$image->resize(200, 200);
			$image->alphaBlending(false);
			$image->saveAlpha(true);
			$image->save($path = $uploadfile);
			chmod($uploadfile, 0777);
			return $name.$type;
		}
	}

}

